//
//  GBWeiboMonitorNameViewController.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/11/8.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "GBWeiboMonitorNameViewController.h"
#import "UniversialTextfield.h"

@interface GBWeiboMonitorNameViewController ()<UITextFieldDelegate>
{
    UniversialTextfield *authornameTextfield; //输入作者名称的textfield
    UIButton *searchBtn; //查询按钮
    UITapGestureRecognizer *tapGestureClick; //点击手势
    UILabel *hintLabel; //提示信息的label
    UIView *view11;
    
    //11.9
    UIButton *deleteBtn; //删除按钮
}
@end

@implementation GBWeiboMonitorNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WQColor(247, 249, 250);
    
    authornameTextfield = [[UniversialTextfield alloc] initWithFrame:CGRectMake(0, 20, screenW, 35)];
    authornameTextfield.backgroundColor = [UIColor whiteColor];
    if(_titleAuthorName == nil)
    {
        //初始化点击手势，以判断是否点击的是除了uitextfield之外的地方
        [self createTapGesture];
        authornameTextfield.placeholder = @"请输入监测名称";
        authornameTextfield.delegate = self;
    }
    else
    {
        authornameTextfield.placeholder = _titleAuthorName;
        //此时nicknameTextfield不可交互
        authornameTextfield.userInteractionEnabled = NO;
    }
    authornameTextfield.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //键盘类型
    authornameTextfield.keyboardType = UIKeyboardTypeDefault;
    //返回按钮类型
    authornameTextfield.returnKeyType = UIReturnKeyDone;
    //改变输入框起始位置
    [self setTextFieldLeftPadding:authornameTextfield forWidth:10];
    [self.view addSubview:authornameTextfield];
    
    searchBtn = [UIButton buttonWithType:0];
    //searchBtn.backgroundColor = KDColor;
    searchBtn.frame = CGRectMake(screenW - 40, 20, 40, 35);
    [searchBtn setImage:[UIImage imageNamed:@"默认添加"] forState:UIControlStateNormal];
    if(_titleAuthorName == nil)
    {
        [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:searchBtn];
    
    view11 = [[UIView alloc] initWithFrame:CGRectMake(0, 55, screenW, 30)];
    
    deleteBtn = [UIButton buttonWithType:0];
    [deleteBtn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
    deleteBtn.frame = CGRectMake(0, 0, 0, 0);
    [deleteBtn addTarget:self action:@selector(DeleteTheTitle) forControlEvents:UIControlEventTouchUpInside];
    
    hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenW, 30)];
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.font = [UIFont systemFontOfSize:12];
    hintLabel.text = @"请输入作者名称";
    [view11 addSubview:hintLabel];
    [view11 addSubview:deleteBtn];
    [self.view addSubview:view11];
    // Do any additional setup after loading the view.
}

#pragma mark - 删除当前title的操作
-(void)DeleteTheTitle
{
    view11.backgroundColor = WQColor(247, 249, 250);
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.text = @"请输入作者名称";
    hintLabel.backgroundColor = WQColor(247, 249, 250);
    deleteBtn.frame = CGRectMake(0, 0, 0, 0);
    authornameTextfield.text = @"";
}

#pragma mark - 昵称中的查询操作
-(void)searchAction
{
    [authornameTextfield resignFirstResponder];
    if(authornameTextfield.text.length != 0)
    {
        view11.backgroundColor = WQColor(233, 239, 242);
        hintLabel.textColor = [UIColor blackColor];
        hintLabel.backgroundColor = WQColor(233, 239, 242);
        hintLabel.text = authornameTextfield.text;
        deleteBtn.frame = CGRectMake(screenW - 50, 0, 30, 30);
    }
    else
    {
        UIAlertView *alertz = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertz show];
    }
}

#pragma mark - 初始化点击手势，以判断是否点击的是除了uitextfield之外的地方
-(void)createTapGesture
{
    //开启交互
    self.view.userInteractionEnabled = YES;
    tapGestureClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapClick)];
    [self.view addGestureRecognizer:tapGestureClick];
}

#pragma mark - 点击除了uitextfield之外的地方，键盘消失
-(void)TapClick
{
    if(authornameTextfield.text.length == 0)
    {
        authornameTextfield.placeholder = @"请输入监测名称";
    }
    [authornameTextfield resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [authornameTextfield resignFirstResponder];
    return YES;
}

-(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth
{
    CGRect frame = textField.frame;
    frame.size.width = leftWidth;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftview;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.delegateWeiboMonitorTitle weiboReturnMonitorTitle:authornameTextfield.text];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
